$(function () {

    layui.use('upload', function(){
        var upload = layui.upload;

        //执行实例
        var uploadInst = upload.render({
            elem: '#typeImg' //绑定元素
            ,url: '/admin/Upload/upload' //上传接口
            ,method: 'post'
            ,accept: 'images' //允许上传的文件类型
            ,size: 200 //最大允许上传的文件大小
            ,done: function(res){
                //上传完毕回调
                if(res.status == 1){
                    $("#imgUrlvalue").val(res.data);
                    $("#test-upload-normal-img").attr("src",res.data);
                }
            }
            ,error: function(){
                //请求异常回调
            }
        });
    });

    layui.use(['form','layer'], function(){
        $ = layui.jquery;
        var form = layui.form
            ,layer = layui.layer;

        //自定义验证规则
        form.verify({
            typeName: function(value){
                if(value == ''){
                    return '分类名称不能为空';
                }
            }
            ,imgUrl: function(value){
                if(value == ''){
                    return '必须上传图片';
                }
            }
        });

        //监听提交
        form.on('submit(add)', function(data){
            //console.log(data);
            //发异步，把数据提交给php
            $.ajax({
                url:save_url,
                data:data.field,
                type:"Post",
                dataType:"json",
                success:function(data){
                    if(data.status == 1){
                        layer.msg("添加成功");
                        var index = parent.layer.getFrameIndex(window.name);
                        //关闭当前frame
                        parent.layer.close(index);
                        parent.location.reload();
                    }
                },
                error:function(data){
                    layer.msg("服务器无响应");
                }
            });

            return false;
        });
    });

});